﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;
using System.Xml;
using DocumentFormat.OpenXml.CustomProperties;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Diagnostics;
using FileManager.Model;



namespace FileManager.Util
{
    internal class OfficeHelper
    {

        public static string GetWordDocumentSource(string filePath)
        {

            using (var package = Package.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                // Locate the core properties part
                Uri corePropertiesUri = new Uri("/docProps/core.xml", UriKind.Relative);
                PackagePart corePropertiesPart = package.GetPart(corePropertiesUri);

                // Load and parse the XML
                XDocument corePropertiesXml = XDocument.Load(XmlReader.Create(corePropertiesPart.GetStream()));
                if(corePropertiesXml.Root != null) {
                    XElement root = corePropertiesXml.Root;
                    XElement creatorElement = null;
                    XElement createdElement = null;
                    foreach (var emp in  root.Elements())
                    {
                        

                        Debug.WriteLine($"{emp.Name}:{emp.Value}");
                    }

                    Debug.WriteLine($"作者:{creatorElement.Value}");
                    Debug.WriteLine($"修改时间{createdElement.Value}");


                }


                XElement modifiedElement = corePropertiesXml.Root.Element(XName.Get("modified", "http://purl.org/dc/terms/"));

                // Extract and return the last save date
                string dateString = modifiedElement.Value;
                return "";
            }
        }
        public static void GetWordDocumentSource(ref MetaData metaData)
        {
            string filePath = metaData.Path;
            string creator = metaData.Author;
            string lastWriter = metaData.LastWriter;
            DateTime modifiedTime = metaData.LastWriteTime;
            DateTime createTime = metaData.CreateTime;

            if (!File.Exists(filePath))
                return;
            Debug.WriteLine($"读取1:{filePath}");
            if (metaData.Extension != ".docx" || metaData.Extension != ".xlsx")
            {
                metaData.IniOfficeInfo();
                return;
            }

            using (var package = Package.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                // Locate the core properties part
                Debug.WriteLine($"读取2:{filePath}");
                Uri corePropertiesUri = new Uri("/docProps/core.xml", UriKind.Relative);
                PackagePart corePropertiesPart = package.GetPart(corePropertiesUri);
                // Load and parse the XML
                XDocument corePropertiesXml = XDocument.Load(XmlReader.Create(corePropertiesPart.GetStream()));

                if (corePropertiesXml.Root != null)
                {
                    XElement root = corePropertiesXml.Root;

                    foreach (var emp in root.Elements())
                    {
                        switch (emp.Name.LocalName)
                        {
                            case "created":
                                createTime= DateTime.Parse(emp.Value, null, System.Globalization.DateTimeStyles.AdjustToUniversal);
                                break;
                            case "creator":
                                creator = emp.Value; 
                                break;
                            case "modified":
                                modifiedTime= DateTime.Parse(emp.Value, null, System.Globalization.DateTimeStyles.AdjustToUniversal);
                                break;
                            case "lastModifiedBy":
                                lastWriter = emp.Value;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            metaData.SetOfficeInfo(creator, lastWriter, createTime, modifiedTime);
        }
    }
}
